package com.chixing.day16_io.reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * A.java --- A.class ---在JVM Class
 * 通过字节码文件得到该类的结构
 * Student:成员变量、     构造函数、     方法
 *         (字段Field) (Constructor) (Method)
 *
 *
 *         Method:修饰符+返回值类型+方法名+参数列表+异常
 *         修饰符(public+abstract+static+final+native)
 */
public class ReflectDemo {
    public static void main(String[] args) {

        Class<Student> clazz = Student.class;

        String className = clazz.getName();
        System.out.println("类名: " + className);
        clazz.getInterfaces();



       // clazz.getMethods();
       Method[] methods = clazz.getDeclaredMethods();  //不包含父类的
        for (Method method:methods){
            Modifier m;
            String methodName = method.getName();
            int modifier = method.getModifiers();
            Class<?> returnType = method.getReturnType();
            Class<?>[] paramTypes = method.getParameterTypes();
            Class<?>[] exceptionTypes = method.getExceptionTypes();

            System.out.print("方法名称" + methodName);
            System.out.print(", 访问修饰符" + modifier);
            System.out.print(", 返回值类型" + returnType);
            for (Class<?> paramType:paramTypes){
                System.out.print(", 参数列表类型:" + paramType);
            }
           for (Class<?> exceptionType:exceptionTypes){
               System.out.println("异常类型" + exceptionType);
           }


        }
        System.out.println("    ");

        //调用study
        Method method = null;
        try {
            method = clazz.getMethod("study", int.class, String.class, Float.class);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

        Object result = null;
        try {
            result = method.invoke(Student.class,10,"aa",9.9f);
            System.out.println("study的返回值:" + result);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        //调用play
       /* Method method1 = null;
        try {
            method1 = clazz.getMethod("play", int.class, float.class);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

        Object result1 = null;
        try {
            result1 = method1.invoke(Student.class,4,3.3f);
            System.out.println("play的返回值" + result1);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }*/


    }

}
